package com.gitee.cui.client.command;

import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * 命令菜单处理类
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/10/27 14:17
 **/
@Data
@Service
public class ClientCommandMenu implements BaseCommand {

    /**
     * 获取菜单的指令
     */
    public static final String KEY = "0";

    /**
     * 所有菜单信息
     */
    private String allCommandShow;

    /**
     * 用户输入的指令信息
     */
    private String commandInput;

    @Override
    public String getKey() {
        return KEY;
    }

    @Override
    public String getTip() {
        return "show 所有命令";
    }

    @Override
    public void exec(Scanner scanner) {
        System.err.println("请输入某个操作指令：" + allCommandShow);
        // 获取第一个指令
        commandInput = scanner.next();
    }

    /**
     * 设置所有命令集合
     * @param commandMap 所有命令集合
     */
    public void setAllCommand(Map<String, BaseCommand> commandMap) {
        Set<Map.Entry<String, BaseCommand>> entries = commandMap.entrySet();
        Iterator<Map.Entry<String, BaseCommand>> iterator = entries.iterator();
        StringBuilder sb = new StringBuilder("[menu] ");
        while (iterator.hasNext()) {
            BaseCommand next = iterator.next().getValue();
            sb.append(next.getKey())
                    .append("->")
                    .append(next.getTip())
                    .append(" | ");
        }
        allCommandShow = sb.toString();
    }
}
